<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Series_active extends Services{
	private $status = TRUE;
	
	function Series_active(){
		parent::__construct();
				
	}
	
	protected function get_active(){
		
		$this->checkAccessToken();
		
		$this->load->model('series_active_model');
		$data	= $this->series_active_model->getSerieActive(self::$params);
			
			if(is_array($data))
				return $this->parseOutput($data);
			else
				throw new NotExistException('Series Activas');
	}	
	
	protected function active_add(){
		
		$this->checkAccessToken();
		$response_validate	= $this->validate('active_add');
		
		if($this->status){
			$this->load->model('series_active_model');
			$data	= $this->series_active_model->saveInfoSerieActive(self::$params);
			
			if(is_array($data))
				return $this->parseOutput($data);
			else
				return $this->parseOutput(array('Error: ' => 'Ya existe una serie activa para esa temporada'));
		}else
			throw new UserValidateException(implode(',',$response_validate));
		
	}
	
	protected function active_update(){
		
		$this->checkAccessToken();
		$response_validate	= $this->validate('active_update');
		
		if($this->status){
			$this->load->model('series_active_model');
			$data	= $this->series_active_model->updateInfoSerieActive(self::$params);
			
			if(is_array($data))
				return $this->parseOutput($data);
			else
				throw new NotExistException('season');
		}else
			throw new UserValidateException(implode(',',$response_validate));
		
	}
	
	protected function active_delete(){
		
		$this->checkAccessToken();
		$response_validate	= $this->validate('active_delete');
		
		if($this->status){
			$this->load->model('series_active_model');
			$data	= $this->series_active_model->deleteSerieActive(self::$params);
			
			if(is_array($data))
				return $this->parseOutput($data);
			else
				throw new NotExistException('serie activa');
		}else
			throw new UserValidateException(implode(',',$response_validate));
		
	}
	
	private function validate_active_add(){
		
		$validate 	= new MY_Form_validation();
		$validate->setArrayValidate(self::$params);
		
		$image_validate = array();
		
		if(!isset($_FILES['image']) && !isset(self::$params['image'])){
			$image_validate = array(
					'field'	=> 'image',
					'label'	=> 'image',
					'rules'	=> 'required'
			);
		}
		else if(isset($_FILES['image']) && isset(self::$params['image'])){
			if(empty($_FILES['image']['name']) && empty(self::$params['image']))
				$image_validate = array(
					'field'	=> 'image',
					'label'	=> 'image',
					'rules'	=> 'required'
				);
		}
		else if(isset($_FILES['image']) && !isset(self::$params['image'])){
			if(empty($_FILES['image']['name']))
				$image_validate = array(
					'field'	=> 'image',
					'label'	=> 'image',
					'rules'	=> 'required'
				);	
		} 
		else if(!isset($_FILES['image']) && isset(self::$params['image'])){
			if(empty(self::$params['image']))
				$image_validate = array(
					'field'	=> 'image',
					'label'	=> 'image',
					'rules'	=> 'required'
				);	
		}
		
		$config = array(
							array(
									'field'	=> 'season',
									'label'	=> 'season',
									'rules'	=> 'required|is_natural_no_zero'
							),
							$image_validate,
							array(
									'field'	=> 'no_order',
									'label'	=> 'no_order',
									'rules'	=> 'required|is_natural_no_zero'
							)
		
				);
				
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		//	VALIDA QUE LA URL DEL $PARAMS['IMAGE'] SEA VALIDA.
		if(isset(self::$params["image"])){
			$url = self::$params['image'];
			if(!sys::urlValid($url)){
    			$this->status = FALSE;
    			$validate->_error_array['values'] = lang('txt_invalid_url', 'image => "'.$url.'"');
			}
    	}
		
		return $validate->_error_array;
	}
	
	private function validate_active_update(){
		
		$validate 	= new MY_Form_validation();
		$validate->setArrayValidate(self::$params);

		$config = array(
							array(
									'field'	=> 'serie_active',
									'label'	=> 'serie_active',
									'rules'	=> 'required|is_natural_no_zero'
							),
							array(
									'field'	=> 'season',
									'label'	=> 'season',
									'rules'	=> 'required|is_natural_no_zero'
							),
							array(
									'field'	=> 'no_order',
									'label'	=> 'no_order',
									'rules'	=> 'required|is_natural_no_zero'
							)
				);
				
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		//	VALIDA QUE LA URL DEL $PARAMS['IMAGE'] SEA VALIDA.
		if(isset(self::$params["image"]) && !empty(self::$params['image'])){
			$url = self::$params['image'];
			if(!sys::urlValid($url)){
    			$this->status = FALSE;
    			$validate->_error_array['values'] = lang('txt_invalid_url', 'image => "'.$url.'"');
			}
    	}
		
		return $validate->_error_array;
	}
	
	
	private function validate_active_delete(){
		$validate	= new MY_Form_validation();
		$validate->setArrayValidate(self::$params);
		
		$config = array(
							array(
									'field' => 'serie_active',
									'label'	=> 'serie_active',
									'rules'	=> 'required|is_natural_no_zero'
							)
				);
		
		$validate->set_rules($config);
		$this->status = $validate->run();
		
		return $validate->_error_array;
	}
	
	
	
	/**
     * Permite incorporar las validaciones bases que apliquen para todos los servicios.
     * Validaciones mas espesificas tendran que ser en cada servicio en particular.
     * @author gama
     */
    private function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
	
	private function parseOutput($data=array(), $only_parse=false){
		if ($only_parse)
			return $this->{'_format_'.$this->getFormat()}($data);
		if($this->status==true){
			return $this->{'_trim_format_'.$this->getFormat()}(
					$this->{'_format_'.$this->getFormat()}($data)
					);
		}
    }
	
}